package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.AdmissionForm;
import com.woniuxy.entity.Tde;
import com.woniuxy.entity.dto.AdmissionDTO;
import com.woniuxy.entity.dto.InHospitalDTO;
import com.woniuxy.entity.dto.QuerySickbedDTO;
import com.woniuxy.mapper.AdmissionFormMapper;
import com.woniuxy.service.AdmissionFormService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.DateUtils;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AdmissionFormServiceImpl implements AdmissionFormService, SaasService {
    @Autowired
    AdmissionFormMapper admissionFormMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResponseData<?> queryList(PageParam<AdmissionDTO> pageParam) {
        //AdmissionDTO param = pageParam.getParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<AdmissionForm> admissionForms = admissionFormMapper.queryList(pageParam.getParam());
        PageInfo pageInfo = new PageInfo(admissionForms);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> queryTdeList() {
        List<Tde> tdeList = (List<Tde>) redisTemplate.opsForHash().get("tds", "min_tds");
        if(tdeList !=null){
            return new ResponseData<>().ok(tdeList);
        }
        List<Tde> tdes = admissionFormMapper.queryTdeList();
        redisTemplate.opsForHash().put("tdes","min_tdes",tdes);
        return new ResponseData<>().ok(tdes);
    }

    @Override
    public ResponseData<?> querySickbed(AdmissionDTO admissionDTO) {
        QuerySickbedDTO querySickbedDTO = admissionFormMapper.querySickbed(admissionDTO.getDeName());
        return new ResponseData<>().ok(querySickbedDTO);
    }

    @Override
    public ResponseData<?> addAdmissionForm(AdmissionForm admissionForm) {
        admissionFormMapper.addAdmissionForm(admissionForm);
        return new ResponseData<>().ok(admissionForm);
    }

    @Override
    public ResponseData<?> updateAdmissionForm(InHospitalDTO inHospitalDTO) {
        inHospitalDTO.setStatus(0);
        admissionFormMapper.updateAdStatus(inHospitalDTO);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryOutList() {
        List<Tde> tdes1 = (List<Tde>) redisTemplate.opsForHash().get("outtdes", "min_outtdes");
        if(tdes1 !=null){
            return new ResponseData<>().ok(tdes1);
        }
        List<Tde> tdes = admissionFormMapper.queryOutList();
        redisTemplate.opsForHash().put("outtdes","min_outtdes",tdes);
        return new ResponseData<>().ok(tdes);
    }


}
